package cn.lysj.framework.common.utils.random;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * @author ZhangWenJian
 * @data 2019/1/16--11:07
 * @description
 */
public class CustomTimeUtil {



    /**
     * @author ZhangWenJian
     * @date 2019/1/2 9:57
     * @title getSkewingTimeInWeek
     * @param weekSkewingNumber 偏移量 正未来时间,负前x周
     * @param weekChoice 星期几的选择
     * @param type 当前的时分秒形式
     * @return java.util.Date 返回该选择日的时间
     * @description  偏移周,依据特定条件获取周一至周日的日期
     */
    public static Date getSkewingTimeInWeek(Date inceptionDate, Integer weekSkewingNumber, WeekChoice weekChoice, CustomTimeType type){
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(inceptionDate);
        calendar.setFirstDayOfWeek(Calendar.MONDAY);
        calendar.add(Calendar.WEEK_OF_YEAR,weekSkewingNumber);
        calendar.set(Calendar.DAY_OF_WEEK,Calendar.MONDAY);
        if(weekChoice.getVal() != 0){
            calendar.set(Calendar.DATE,calendar.get(Calendar.DATE)+weekChoice.getVal());
        }
        //设置时间为一天的最开始或者最结尾
        changeCalendarHour(calendar,type);
        return calendar.getTime();
    };

    /**
     * @author ZhangWenJian
     * @date 2019/1/2 14:04
     * @title getSkewingTimeInDay
     * @param daySkewingNumber 偏移天数
     * @param type 当前的时分秒形式
     * @return java.util.Date
     * @description
     */
    public static Date getSkewingTimeInDay(Date inceptionDate, Integer daySkewingNumber, CustomTimeType type){
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(inceptionDate);
        calendar.set(Calendar.DATE,calendar.get(Calendar.DATE)+daySkewingNumber);
        //设置时间为一天的最开始或者最结尾
        changeCalendarHour(calendar,type);
        return calendar.getTime();
    }
    /**
     * @author ZhangWenJian
     * @date 2019/1/2 14:37
     * @title getNaturalMonth
     * @param inceptionDate 起始时间
     * @param addMouth 加减月份
     * @param type 当前的时分秒形式
     * @return java.util.Date
     * @description
     */
    public static Date getSkewingTimeInMonth(Date inceptionDate, Integer addMouth, CustomTimeType type){
        Calendar calendar = Calendar.getInstance();//获得一个日历的实例
        calendar.setTime(inceptionDate);//设置日历时间
        calendar.add(Calendar.MONTH, addMouth);//在日历的月份上增加 addMouth
        //设置时间为一天的最开始或者最结尾
        changeCalendarHour(calendar,type);
        return calendar.getTime();//得到 addMouth 月后的日期
    }

    public static Date getSkewingTimeInYear(Date inceptionDate, Integer addYear, CustomTimeType type){
        Calendar calendar = Calendar.getInstance();//获得一个日历的实例
        calendar.setTime(inceptionDate);//设置日历时间
        calendar.add(Calendar.YEAR, addYear);//在日历的月份上增加 addMouth
        //设置时间为一天的最开始或者最结尾
        changeCalendarMonth(calendar,type);
        return calendar.getTime();//得到 addMouth 月后的日期
    }
    public static void changeCalendarMonth(Calendar calendar, CustomTimeType type){
        if(type.getVal() != 0){
            calendar.set(Calendar.MONTH,0);
            calendar.set(Calendar.DAY_OF_YEAR,1);
            calendar.set(Calendar. HOUR_OF_DAY, 0);
            calendar.set(Calendar. MINUTE, 0);
            calendar.set(Calendar. SECOND, 0);
            if(type.getVal() == 2){
                calendar.set(Calendar. MILLISECOND, -1);
                calendar.add(Calendar. YEAR, 1);
            }
        }
    }
    /**
     * @author ZhangWenJian
     * @date 2019/1/2 14:00
     * @title changeCalendar
     * @param calendar
     * @param type
     * @return void
     * @description  修改calendar时间类
     */
    public static void changeCalendarHour(Calendar calendar, CustomTimeType type){
        if(type.getVal() != 0){
            calendar.set(Calendar. HOUR_OF_DAY, 0);
            calendar.set(Calendar. MINUTE, 0);
            calendar.set(Calendar. SECOND, 0);
            calendar.set(Calendar. MILLISECOND,0);
            if(type.getVal() == 2){
                calendar.set(Calendar.HOUR_OF_DAY, 23);
                calendar.set(Calendar.MINUTE, 59);
                calendar.set(Calendar.SECOND, 59);
                calendar.set(Calendar.MILLISECOND,0);
            }
        }
    }

    /**
     * @author ZhangWenJian
     * @date 2019/1/18 16:05
     * @title getSysYear
     * @param
     * @return java.lang.String
     * @description 获取当年年份
     */
    public static String getSysYear() {

        Calendar date = Calendar.getInstance();

        String year = String.valueOf(date.get(Calendar.YEAR));

        return year;

    }
    /**
     * @author ZhangWenJian
     * @date 2019/1/25 10:17
     * @title changeBirthday
     * @param changeTime
     * @param birthday
     * @return java.util.Date
     * @description 用于生日判断
     */
    public static Date changeBirthday(Date changeTime,Date birthday){
        Calendar calendar = Calendar.getInstance();//获得一个日历的实例
        calendar.setTime(birthday);//设置日历时间
        Integer year = calendar.get(Calendar.YEAR);
        calendar.setTime(changeTime);
        calendar.set(Calendar.YEAR,year);
        return calendar.getTime();
    }

}
